package com.doit.day05

/**
 * sql:
 * case id
 * when 1 then ...
 * when 2 then ...
 * else ... end
 *
 *
 * case
 * when id = 1 then
 * when id >1 then
 * else ... end
 */
object _06_模式匹配 {
  def main(args: Array[String]): Unit = {


    val list: List[Any] = List[Any](1, "hello", true, 'a', 4, 5, 6)

    val strings: List[String] = list.collect {
      case a if a.isInstanceOf[Int] => a.asInstanceOf[Int] * 10 + ""
      case a if a.isInstanceOf[String] => a.asInstanceOf[String] + "ruirui"
      case _ => "xiaorui"
    }
    println(strings)



    //解构匹配的变形应用，定义变量
    val (id1,name2) = (1,2)

    println(id1)
    println(name2)

    val map: Map[Int, String] = Map((1, "zss"), (2, "ls"))
    for ((key,_) <- map) {
      println(key)
    }





    //守卫模式  ==》for 模式守卫
    //值匹配
    val name: String = "hongRui"
    val str: String = name match {
      case "hongRui1" => "祝你前程似锦"
      case "lss" => "22k等着你"
      case "liuyan" => "你懂得！！"
      case _ => "98k等你"
    }
    println(str)

    val num = 10
    num match {
      case abc: Int if abc < 5 => println("我小于5")
      case abc: Int if abc > 5 && abc < 10 => println("我是5-10")
      case abc: Int if abc >= 10 => println("我是大于等于10")
      case _ => println("啥也不是")
    }


    //匹配类型
    val tp: Array[Any] = Array((1, 2), 1, "zss", true, Array(1, 2, 3), List(1, 2, 3), Array("a"), List("a"))
    for (elem <- tp) {
      elem match {
        case (1, 2) => println("这是一个二元元祖，元祖的第一个值必须得是1，第二个值必须得是2")
        case (1, _) => println("这是一个二元元祖，元祖的第一个值必须得是1，第二个是啥都行")
        case a: String => println("我是字符串")
        case a: Int => println("我是Int类型")
        case a: Double => println("我是Double类型")
        //数组不会泛型擦除
        case a: Array[Int] => println("我是数组")
        //List在进行判断的时候，会泛型擦除
        case a: List[Int] => println("我是List")
        case _ => println("我也不知道我是什么类型")
      }
    }


    println("====================")


    val array = Array(Array(0), Array(1, 0), Array(0, 1, 0),
      Array(1, 1, 0), Array(1, 1, 0, 1), Array("hello", 90))
    //匹配数组
    for (arr <- array) { // 对
      //      一个数组集合进行遍历
      val result = arr match {
        case Array(0,1) => "0" //匹配 Array(0) 这个数组
        case Array(x, y , z) => x + "," + y+","+z //匹配有两个元素的数组，然后将将元素值赋给对应的 x,y
        case Array(0, _*) => "以 0 开头的数组" //匹配以 0 开头和
        case Array(1, _) => "首先数组两个元素，第一个是0，第二个不关心"
        //          数组
        case _ => "something else"
      }
      println(result)
    }

    //匹配元祖
    for (tuple <- Array((0, 1), (1, 0), (1, 1), (1, 0, 2))) {
      val result = tuple match {
        case (0, _) => "0 ..." //是第一个元素是 0 的元组
        case (y, 0) => "" + y + "0" // 匹配后一个元素是 0 的对偶元组
        case (a, b) => "" + a + " " + b
        case _ => "something else" //默认
      }
      println(result)
    }


    List(1,2,3)

    1 :: 2 :: 3 :: Nil

    //匹配列表
    for (list <- Array(List(0), List(2, 1), List(0, 0, 0), List(1,
      0, 0), List(88))) {
      val result = list match {
        case List(0) => "0" //匹配 List(0)
        case 2::1::Nil =>  "List(2,1)"//匹配有两个元素的 List
        case List(0, _*) => "0 ..."
        case _ => "something else"
      }
      println(result)
    }


    //结构匹配==》 将对象拆成零部件

    val abc: Lion = Lion(1, "辛巴", 2)
    val bcd: Lion1 = new Lion1(1, "辛巴", 2)

    bcd match {
      case Lion1(1,b,c) => println("id等于1的狮子")
      case Lion1(_,"星巴克",c) => println("name=星巴克的狮子")
      case Lion1(_,"小狮子",3) => println("id等于1的狮子")
      case _ => println("其他的狮子")
    }




  }
}

case class Lion(id:Int,name:String,age:Int)

class Lion1(val id:Int,val name:String,val age:Int)

object Lion1{
  def apply(id: Int, name: String, age: Int): Lion1 = new Lion1(id, name, age)

  def unapply(arg: Lion1): Option[(Int, String, Int)] = Some(arg.id, arg.name, arg.age)

}




